package cn.tt.tuantuanerp.tuantuanerp.product.webapi.utils;

import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.Sku;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.math.BigDecimal;

@Slf4j
public class SkuApi {

    private static final String APP_ID="llsjm6sutsx9uepi";
    private static final String APP_SECRET="YjNPOVdXQWRNMVJ3QXVsZnVvamJldz09";

    public static Sku getSkuInfoByApi(Long skuId, Sku defaultSku){
        HttpGet httpGet = new HttpGet("https://www.mxnzp.com/api/barcode/goods/details?barcode="+skuId);
        httpGet.setHeader("app_id",APP_ID);
        httpGet.setHeader("app_secret",APP_SECRET);
        defaultSku.setSkuId(skuId);
        defaultSku.setSkuStatus(3);
        // (3) 发送请求
        HttpClient http = new DefaultHttpClient();
        HttpResponse response = null;
        try {
            response = http.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                log.debug("获取sku信息成功");
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                String data = message.substring(message.indexOf("data")+6);
                data=data.substring(0,data.length()-1);
                System.out.println(data);
                String goodsName = data.substring(data.indexOf("goodsName")+12,data.indexOf("barcode")-3);
                String price = data.substring(data.indexOf("price")+8,data.indexOf("brand")-3);
                if (price.length()==0){
                    price="0.00";
                }
                String brand = data.substring(data.indexOf("brand")+8,data.indexOf("supplier")-3);
                String standard = data.substring(data.indexOf("standard")+11,data.length()-2);
                defaultSku.setSkuName(goodsName+" "+standard);
                defaultSku.setGoodsPrice(new BigDecimal(price.toString()));
                defaultSku.setBrand(brand);
            } else {
                defaultSku.setSkuName("自动创建的商品");
                defaultSku.setGoodsPrice(new BigDecimal("0.00"));
            }
        } catch (IOException e) {
        }
        log.debug("生成sku:{}",defaultSku);
        return defaultSku;
    }

    public static Sku getSkuInfoByApiForAddNewSku(Long skuId){
        HttpGet httpGet = new HttpGet("https://www.mxnzp.com/api/barcode/goods/details?barcode="+skuId);
        httpGet.setHeader("app_id",APP_ID);
        httpGet.setHeader("app_secret",APP_SECRET);
        // (3) 发送请求
        HttpClient http = new DefaultHttpClient();
        HttpResponse response = null;
        try {
            response = http.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                log.debug("获取sku信息成功");
                HttpEntity resEntity = response.getEntity();
                String message = EntityUtils.toString(resEntity, "utf-8");
                System.out.println(message);
                String code = message.substring(message.indexOf("code")+6,message.indexOf("code")+7);
                System.out.println("是否收录此商品:"+code);
                if (code.equals("0")){
                    return null;
                }
                String goodsName = null;
                String price = null;
                String brand = null;
                String standard = null;
                try {
                    String data = message.substring(message.indexOf("data")+6);
                    data=data.substring(0,data.length()-1);
                    System.out.println(data);
                    goodsName = data.substring(data.indexOf("goodsName")+12,data.indexOf("barcode")-3);
                    price = data.substring(data.indexOf("price")+8,data.indexOf("brand")-3);
                    log.debug("price的长度:{}",price.length());
                    if (price.length()==0){
                        price="0.00";
                    }

                    brand = data.substring(data.indexOf("brand")+8,data.indexOf("supplier")-3);
                    standard = data.substring(data.indexOf("standard")+11,data.length()-2);
                } catch (StringIndexOutOfBoundsException e) {
                    return null;
                }
                Sku sku = new Sku();
                sku.setSkuName(goodsName+" "+standard);
                sku.setGoodsPrice(new BigDecimal(price.toString()));
                sku.setBrand(brand);
                return sku;
            }
        } catch (IOException e) {
        }
        return null;
    }

}
